/*
 *
 * This file is part of java2XML
 * Copyright (C) Harsh Jain, All Rights Reserved.
 * Email : harsh@harshjain.com                        Website : http://www.harshjain.com/
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the
 * use of this software.
 *
 * Permission is granted to anyone to use this software for any non-commercial
 * applications freely, subject to the following restriction.
 *
 *  1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software in
 *     a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *
 *  2. Altered source versions must be plainly marked as such, and must not be
 *     misrepresented as being the original software.
 *
 *  3. This notice must not be removed or altered from any source distribution.
 *
 * For using this software for commercial purpose please contact the author.
 *
 *
 */
package harsh.javatoxml;

import harsh.javatoxml.Exceptions.JXMLException;

import harsh.javatoxml.data.*;
import harsh.javatoxml.dom.Domifier;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Writer;

import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;


/**
 * XML Helper class. This class deals with XML Stuff.
 *
 * @author Harsh Jain
 */
public class XMLHelper {
    
    /**
     * Dumps the IXMLElement to Output Stream
     *
     * @param element IXMLElement the in memory representation
     * @param out Output stream
     *
     * @throws JXMLException On IOException
     */
    public static void dumpXMLElement(IXMLElement element, PrintStream out)
        throws JXMLException {
        try {
            out.println("<!-- Generated by Java2XML http://java2xml.dev.java.net/ -->");
            out.flush();

            XMLWriter writer = new XMLWriter(out);
            writer.write(element, true);
        } catch (IOException e) {
            throw new JXMLException(e.getMessage());
        }
    }

    /**
     * Dumps the IXMLElement to the Writer
     *
     * @param element DOCUMENT ME!
     * @param w DOCUMENT ME!
     *
     * @throws JXMLException DOCUMENT ME!
     */
    public static void dumpXMLElement(IXMLElement element, Writer w)
        throws JXMLException {
        try {
            w.write("<!-- Generated by Java2XML http://java2xml.dev.java.net/ -->");
            w.write("\n");
            w.flush();

            XMLWriter writer = new XMLWriter(w);
            writer.write(element, true);
        } catch (IOException e) {
            throw new JXMLException(e.getMessage());
        }
    }

    /**
     * Create a copy of IXMLElement
     *
     * @param elem 
     *
     * @return a new memory copy
     */
    public static IXMLElement createCopy(IXMLElement elem) {
        IXMLElement copy = new XMLElement();
        copy.setName(elem.getName());

        Properties p = elem.getAttributes();
        Enumeration allKeys = p.keys();

        while (allKeys.hasMoreElements()) {
            String key = (String) allKeys.nextElement();
            copy.setAttribute(key, elem.getAttribute(key));
        }

        Vector children = elem.getChildren();

        for (int i = 0; i < children.size(); i++) {
            IXMLElement child = (IXMLElement) children.elementAt(i);
            copy.addChild(createCopy(child));
        }

        return copy;
    }
    
    public static void dumpXMLToFile(IXMLElement element, String fileName)  throws JXMLException {
    	try {
    	Source source = new DOMSource(Domifier.toDom(element));
        File file = new File(fileName);
        Result result = new StreamResult(file);
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
		} catch (Exception e) {
			throw new JXMLException(e.getLocalizedMessage());
		} 
    	
    }
}
